package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.AddCartForm;
import com.cskaoyan.gateway.form.shopping.UpdateShoppingCartForm;
import com.google.gson.Gson;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.JwtTokenUtils;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import com.mall.user.dto.UsernameIdToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/*
 * @author: ckd
 * @clsss: ShoppingCartsController
 */
@SuppressWarnings("all")
@Anoymous
@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "ShoppingCartsController", description = "商城购物车模块")
public class ShoppingCartsController {

    @Reference(interfaceClass = ICartService.class, timeout = 100000, retries = 0, check = false)
    ICartService iCartServicel;

    @GetMapping("/carts")
    public ResponseData getCartListById(CartListByIdRequest request, HttpServletRequest httpServletRequest) {
        Cookie[] cookies = httpServletRequest.getCookies();
        String token = null;
        for (Cookie cookie : cookies) {
            if ("access_token".equals(cookie.getName())) {
                token = cookie.getValue();
            }
        }
        String jwtToken = JwtTokenUtils.builder().token(token).build().freeJwt();
        Gson gson = new Gson();
        UsernameIdToken usernameIdToken = gson.fromJson(jwtToken, UsernameIdToken.class);
        Long userId = usernameIdToken.getUserId();
        request.setUserId(userId);

        CartListByIdResponse cartListById = iCartServicel.getCartListById(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(cartListById.getCode())) {
            return new ResponseUtil().setData(cartListById.getCartProductDtos());
        }

        return new ResponseUtil().setErrorMsg(cartListById.getMsg());
    }

    @PostMapping("carts")
    public ResponseData addCart(@RequestBody AddCartForm addCartForm) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(addCartForm.getUserId());
        addCartRequest.setItemId(addCartForm.getProductId());
        addCartRequest.setNum(addCartForm.getProductNum());
        AddCartResponse addCartResponse = iCartServicel.addToCart(addCartRequest);
        //System.out.println(addCartResponse);
        if (ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }

    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody UpdateShoppingCartForm updateShoppingCartForm) {
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(updateShoppingCartForm.getUserId());
        updateCartNumRequest.setItemId(updateShoppingCartForm.getProductId());
        updateCartNumRequest.setNum(updateShoppingCartForm.getProductNum());
        updateCartNumRequest.setChecked(updateShoppingCartForm.getChecked());
        UpdateCartNumResponse updateCartNumResponse = iCartServicel.updateCartNum(updateCartNumRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteShoppingCart(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = iCartServicel.deleteCartItem(deleteCartItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }
//
//    @DeleteMapping("items/{id}")
//    @ApiOperation("删除购物⻋中选中的商品")
//    public ResponseData deleteCheckedItem(HttpServletRequest request) {
//
//        String token = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject tokenJson = JSON.parseObject(token);
//        Long uid = tokenJson.getLong("uid");
//        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
//        deleteCheckedItemRequest.setUserId(uid);
//
//        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartServicel.deleteCheckedItem(deleteCheckedItemRequest);
//
//        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
//            return new ResponseUtil<String>().setData("成功", deleteCheckedItemResposne.getMsg());
//        }
//    }

    @DeleteMapping("/items/{id}")
    @ApiOperation("删除购物车中所选中的商品")
    public ResponseData deleteCheckedItembyId(@PathVariable(name = "id") long id) {
        DeleteCheckedItemRequest checkedItemRequest = new DeleteCheckedItemRequest();

        checkedItemRequest.setUserId(id);
        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartServicel.deleteCheckedItem(checkedItemRequest);


        if (deleteCheckedItemResposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            ResponseData responseData = new ResponseUtil<>().setData("成功");
            return responseData;

        }
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }
}
